package aaa.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 *
 * @author		Zanette Sébastien
 */
public class CreateDatabases extends SQLiteOpenHelper implements DatabaseConstants {

	private static final String CREATE_SCHEDULE_BDD = 
			String.format("CREATE TABLE IF NOT EXISTS %s (%s TEXT PRIMARY KEY, %s INTEGER NOT NULL, %s TEXT NOT NULL, %s INTEGER NOT NULL, %s DATETIME, %s INT NOT NULL, %s DATETIME, %s INT NOT NULL) ;", 
					TABLE_SCHEDULE,
					SCH_COL_NAME,
					SCH_COL_PROJECT_ID,
					SCH_COL_RESSOURCE_ID,
					SCH_COL_PERIOD,
					SCH_COL_LAST_MODIF,
					SCH_COL_NOTIFY,
					SCH_COL_CREATED,
					SCH_COL_STATUS) ;

	private static final String CREATE_LESSON_BDD = 
			String.format("CREATE TABLE IF NOT EXISTS %s (%s INTEGER NOT NULL, %s VARCHAR(100) NOT NULL, %s DATETIME, %s VARCHAR(30) NOT NULL, %s INTEGER NOT NULL, %s DATETIME, %s VARCHAR(20), %s VARCHAR(30) NOT NULL, %s VARCHAR(20) NOT NULL, PRIMARY KEY (%s, %s)) ;", 
					TABLE_LESSON,
					LES_COL_LESSON_ID,
					LES_COL_TITLE,
					LES_COL_TIME,
					LES_COL_SCHEDULE,
					LES_COL_TYPE,
					SCH_COL_LAST_MODIF,
					LES_COL_ROOM,
					LES_COL_GROUP,
					LES_COL_TEACHER,
					LES_COL_LESSON_ID,
					LES_COL_SCHEDULE) ;

	public static final String CREATE_INSTITUTION_BDD = 
			String.format("CREATE TABLE IF NOT EXISTS %s (%s INTEGER NOT NULL, %s VARCHAR(100) NOT NULL, PRIMARY KEY (%s)) ;", 
					TABLE_INSTITUTION,
					INST_COL_ID,
					INST_COL_NAME,
					INST_COL_ID) ;

	public static final String CREATE_ROOM_BDD = 
			String.format("CREATE TABLE IF NOT EXISTS %s (%s INTEGER NOT NULL, %s INTEGER NOT NULL, %s VARCHAR(100) NOT NULL, PRIMARY KEY (%s)) ;",
					TABLE_ROOM,
					ROOM_COL_ID,
					ROOM_COL_INSTITUTION,
					ROOM_COL_NAME,
					ROOM_COL_ID);

	public CreateDatabases(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version) ;
		this.onCreate(context, this.getWritableDatabase()) ;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_SCHEDULE_BDD);
		db.execSQL(CREATE_LESSON_BDD);
		db.execSQL(CREATE_INSTITUTION_BDD);
		db.execSQL(CREATE_ROOM_BDD);
	}
	
	public void onCreate(Context context, SQLiteDatabase db) {
		this.onCreate(db) ;
		InitialLocations.testDatabase(context, this) ;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		throw new UnsupportedOperationException("Not supported yet.");
	}
	
}
